<template>
  <el-dialog
    :close-on-click-modal="false"
    title="易耗品台账"
    :visible.sync="show"
    width="1000px"
    center
  >
    <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="100px">
      <div class="com-form-title">基本信息</div>
      <el-row>
        <el-col :span="12">
          <el-row>
            <el-col :span="12">
              <el-form-item label="序号" prop="sequence">
                <el-input v-model="form.sequence" :disabled="disableForm" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="资产编号" prop="assetNumber">
                <el-input v-model="form.assetNumber" :disabled="disableForm" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="资产单位" prop="assetUnit">
                <el-input v-model="form.assetUnit" :disabled="disableForm" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="车牌号" prop="plateNumber">
                <el-input v-model="form.plateNumber" :disabled="disableForm" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="12">
          <el-row>
            <el-col :span="12">
              <el-form-item label="设备编号" prop="devNumber">
                <el-input v-model="form.devNumber" :disabled="disableForm" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="设备名称" prop="devName">
                <el-input v-model="form.devName" :disabled="disableForm" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="设备分类" prop="devCategory">
                <el-input v-model="form.devCategory" :disabled="disableForm" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="设备状态" prop="devStatus">
                <el-select v-model="form.devStatus" :disabled="disableForm" style="width: 100%;" placeholder="请选择设备状态">
                  <el-option v-for="item in consumableLedgerDevStatus" :key="item.name" :label="item.name" :value="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="12">
          <el-row>
            <el-col :span="12">
              <el-form-item label="设备来源" prop="devSource">
                <el-select v-model="form.devSource" :disabled="disableForm" style="width: 100%;" placeholder="请选择设备来源">
                  <el-option v-for="item in consumableLedgerDevSource" :key="item.name" :label="item.name" :value="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="设备功率" prop="devPower">
                <el-input v-model="form.devPower" :disabled="disableForm" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="规格" prop="specification">
                <el-input v-model="form.specification" :disabled="disableForm" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="所在地" prop="address">
                <el-input v-model="form.address" :disabled="disableForm" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="12">
          <el-row>
            <el-col :span="12">
              <el-form-item label="技术状况" prop="techStatus">
                <el-select v-model="form.techStatus" :disabled="disableForm" style="width: 100%;" placeholder="请选择技术状况">
                  <el-option v-for="item in consumableLedgerTechStatus" :key="item.name" :label="item.name" :value="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="厂家编号" prop="factoryNumber">
                <el-input v-model="form.factoryNumber" :disabled="disableForm" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="厂家名称" prop="factoryName">
                <el-input v-model="form.factoryName" :disabled="disableForm" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="出厂时间" prop="factoryTime">
                <el-date-picker v-model="form.factoryTime" style="width:100%" type="date" :disabled="disableForm" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
      <div class="com-form-title">资产信息</div>
      <el-row>
        <el-col :span="12">
          <el-row>
            <el-col :span="12">
              <el-form-item label="设计能力" prop="designAbility">
                <el-input v-model="form.designAbility" :disabled="disableForm" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="购买时间" prop="payTime">
                <el-date-picker v-model="form.payTime" style="width:100%" type="date" :disabled="disableForm" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="机长" prop="captain">
                <el-input v-model="form.captain" :disabled="disableForm" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="外形尺寸" prop="size">
                <el-input v-model="form.size" :disabled="disableForm" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="12">
          <el-row>
            <el-col :span="12">
              <el-form-item label="发动机编号" prop="engineNumber">
                <el-input v-model="form.engineNumber" :disabled="disableForm" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="重量" prop="weight">
                <el-input v-model="form.weight" :disabled="disableForm" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="底盘号" prop="chassisNumber">
                <el-input v-model="form.chassisNumber" :disabled="disableForm" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否监视测量仪器" prop="monitorDevice">
                <el-select v-model="form.monitorDevice" :disabled="disableForm" style="width: 100%;" placeholder="请选择是否监视测量仪器">
                  <el-option v-for="item in consumableLedgerMonitorDevice" :key="item.name" :label="item.name" :value="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="12">
          <el-row>
            <el-col :span="12">
              <el-form-item label="是否特种设备" prop="specialDevice">
                <el-select v-model="form.specialDevice" :disabled="disableForm" style="width: 100%;" placeholder="请选择是否特种设备">
                  <el-option v-for="item in consumableLedgerSpecialDevice" :key="item.name" :label="item.name" :value="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否大型专用设备" prop="professionalDevice">
                <el-select v-model="form.professionalDevice" :disabled="disableForm" style="width: 100%;" placeholder="请选择是否大型专用设备">
                  <el-option v-for="item in consumableLedgerProfessionalDevice" :key="item.name" :label="item.name" :value="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" :disabled="disableForm" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="购置原值" prop="payAmount">
                <el-input v-model="form.payAmount" :disabled="disableForm" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="12">
          <el-row>
            <el-col :span="12">
              <el-form-item label="重置原值" prop="resetAmount">
                <el-input v-model="form.resetAmount" :disabled="disableForm" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="规定折旧月数" prop="oldMonth">
                <el-input v-model="form.oldMonth" :disabled="disableForm" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="使用月数" prop="useMonth">
                <el-input v-model="form.useMonth" :disabled="disableForm" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="净值" prop="netValue">
                <el-input v-model="form.netValue" :disabled="disableForm" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
      <div class="com-form-title">辅机信息</div>
      <el-row>
        <el-col :span="12">
          <el-row>
            <el-col :span="12">
              <el-form-item label="残值" prop="residualValue">
                <el-input v-model="form.residualValue" :disabled="disableForm" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="残值率" prop="residualRate">
                <el-input v-model="form.residualRate" :disabled="disableForm" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="剩余折旧月数" prop="remainMonth">
                <el-input v-model="form.remainMonth" :disabled="disableForm" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="累计折旧值" prop="totalOldValue">
                <el-input v-model="form.totalOldValue" :disabled="disableForm" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="12">
          <el-row>
            <el-col :span="12">
              <el-form-item label="辅机名称" prop="amName">
                <el-input v-model="form.amName" :disabled="disableForm" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="辅机型号" prop="amModel">
                <el-input v-model="form.amModel" :disabled="disableForm" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="辅机厂家" prop="amFactory">
                <el-input v-model="form.amFactory" :disabled="disableForm" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="辅机功率" prop="amPower">
                <el-input v-model="form.amPower" :disabled="disableForm" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="12">
          <el-row>
            <el-col :span="12">
              <el-form-item label="辅机编号" prop="amNumber">
                <el-input v-model="form.amNumber" :disabled="disableForm" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="辅机出厂时间" prop="amTime">
                <el-date-picker v-model="form.amTime" style="width:100%" type="date" :disabled="disableForm" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="图片" prop="image">
                <el-input v-model="form.image" :disabled="disableForm" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="编制时间" prop="createTime">
                <el-date-picker v-model="form.createTime" style="width:100%" type="date" :disabled="disableForm" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
      </el-row>

      <div class="com-form-title">附件</div>
      <el-button style="margin-top:10px;" type="primary" @click="showFileModal = true">附件资料({{ fileList.length }})</el-button>
      <file-uploader v-model="fileList" :show-modal.sync="showFileModal" :disabled="disableForm" :can-download="optType==='detail'" />
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="saveDoc">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  // 详情模块
  addMachineDeviceDetail,
  // deleteMachineDeviceDetail,
  getMachineDeviceDetailDetail,
  // getMachineDeviceDetailList,
  updateMachineDeviceDetail

} from '@/api/basicCode'
import { getAreaList } from '@/api/common'
import { consumableLedgerDevStatus, consumableLedgerDevSource, consumableLedgerTechStatus, consumableLedgerMonitorDevice, consumableLedgerSpecialDevice, consumableLedgerProfessionalDevice } from '@/utils/dicData'
import fileUploader from '@/components/FileUploader'
export default {
  name: 'AddConsumableLedger',
  components: {
    fileUploader
  },
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    optType: {
      type: String,
      default: 'add'
    },
    dir: {
      type: Object,
      default() {
        return {}
      }
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    },
    treedata: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      showFileModal: false,
      selectRow: null,
      targetList: [],
      consumableLedgerDevStatus,
      consumableLedgerDevSource,
      consumableLedgerTechStatus,
      consumableLedgerMonitorDevice,
      consumableLedgerSpecialDevice,
      consumableLedgerProfessionalDevice,

      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      form: {
        machineDeviceDirectoryId: '',
        sequence: '',
        assetNumber: '',
        assetUnit: '',
        plateNumber: '',
        devNumber: '',
        devName: '',
        devCategory: '',
        devStatus: '',
        devSource: '',
        devPower: '',
        specification: '',
        address: '',
        techStatus: '',
        factoryNumber: '',
        factoryName: '',
        factoryTime: '',
        designAbility: '',
        payTime: '',
        captain: '',
        size: '',
        engineNumber: '',
        weight: '',
        chassisNumber: '',
        monitorDevice: '',
        specialDevice: '',
        professionalDevice: '',
        remark: '',
        payAmount: '',
        resetAmount: '',
        oldMonth: '',
        useMonth: '',
        netValue: '',
        residualValue: '',
        residualRate: '',
        remainMonth: '',
        totalOldValue: '',
        amName: '',
        amModel: '',
        amFactory: '',
        amPower: '',
        amNumber: '',
        amTime: '',
        image: '',
        createTime: ''
      },
      fileList: [
      ],
      rules: {
        name: [
          { required: true, message: '单位名称', trigger: 'change' }
        ],
        unifiedSocialCreditCode: [
          { required: true, message: '统一社会信用代码', trigger: 'change' }
        ]
      },
      provinceData: [],
      cityData: []
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    },
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
      }
    }
  },
  watch: {
    showModal(val) {
      if (val) {
        if (this.optType === 'edit') {
          this.getDocDetail(this.data.id)
          this.$nextTick(() => {
            Object.keys(this.form).forEach(key => {
              const source = this.data[key]
              if (typeof source === 'boolean') {
                this.form[key] = source ? 1 : 0
              } else {
                this.form[key] = source
              }
            })
          })
        }
      } else if (!val) {
        this.fileList = []
        this.$refs.form.resetFields()
      }
    }
  },
  mounted() {
    this.getRegionData()
  },
  methods: {
    provinceChange(e) {
      const selectedData = this.provinceData.filter(val => val.name === e)[0]
      if (selectedData) {
        this.form.city = ''
        const params = {
          parentCode: selectedData.code,
          areaType: 2
        }
        getAreaList(params).then(res => {
          this.cityData = res.data || []
        })
      }
    },
    getDocDetail(id) {
      getMachineDeviceDetailDetail({ machineDeviceDetailId: id }).then(res => {
        const { lstAttachment } = res.data || {}
        if (lstAttachment && lstAttachment.length > 0) {
          this.fileList = lstAttachment
        }
      })
    },
    getRegionData() {
      getAreaList({ areaType: '1' }).then(res => {
        this.provinceData = res.data || []
      })
    },
    saveDoc() {
      this.form.machineDeviceDirectoryId = this.treedata.id
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          const params = {
            machineDeviceDetail: {
              ...this.form,
              ...this.optType === 'edit' ? {
                id: this.data.id
              } : {}
            },
            lstAttachment: this.fileList
          }
          const fn = this.optType === 'add' ? addMachineDeviceDetail : updateMachineDeviceDetail
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
              this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}
</style>
